package com.it.schoolbookshop_back.controller.admin;

import com.it.schoolbookshop_back.entities.po.Report;
import com.it.schoolbookshop_back.entities.po.Result;
import com.it.schoolbookshop_back.enums.UserRoleEnums;
import com.it.schoolbookshop_back.service.ReportService;
import com.it.schoolbookshop_back.utils.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员举报管理控制器
 */
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/api/admin")
public class AdminReportsController {

    @Autowired
    private ReportService reportService;
    
    /**
     * 检查用户是否拥有管理员权限
     * @return 是否有管理员权限
     */
    private boolean hasAdminPermission() {
        String userRole = UserContext.getRole();
        String adminRole = UserRoleEnums.ADMIN.name();
        // 不区分大小写比较角色
        return userRole != null && userRole.equalsIgnoreCase(adminRole);
    }
    
    /**
     * 管理员查看所有举报信息
     * 
     * @return 所有举报记录
     */
    @GetMapping("/reports")
    public Result<Map<String, Object>> getAllReports() {
        // 检查权限
        if (!hasAdminPermission()) {
            return Result.failed(403, "权限不足，仅管理员可查看所有举报信息");
        }
        
        try {
            log.info("管理员查询所有举报信息");
            List<Report> reports = reportService.getAllReports();
            
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("reports", reports);
            
            return Result.success("获取举报列表成功", resultMap);
        } catch (Exception e) {
            log.error("获取举报列表失败: {}", e.getMessage(), e);
            return Result.failed("获取举报列表失败: " + e.getMessage());
        }
    }
    
    /**
     * 处理举报
     * 
     * @param reportId 举报ID
     * @param status 处理状态 (processed 或 rejected)
     * @param adminComment 管理员备注
     * @return 处理结果
     */
    @PutMapping("/reports/{id}")
    public Result<String> processReport(
            @PathVariable("id") Integer reportId,
            @RequestParam("status") String status,
            @RequestParam(value = "comment", required = false) String adminComment) {
        
        // 检查权限
        if (!hasAdminPermission()) {
            return Result.failed(403, "权限不足，仅管理员可处理举报");
        }
        
        // 验证参数
        if (reportId == null) {
            return Result.validateFailed("举报ID不能为空");
        }
        
        if (status == null || (!status.equals("processed") && !status.equals("rejected"))) {
            return Result.validateFailed("状态无效，必须为 'processed' 或 'rejected'");
        }
        
        try {
            boolean success = reportService.processReport(reportId, status, adminComment);
            
            if (success) {
                log.info("管理员成功处理举报, ID: {}, 状态: {}", reportId, status);
                return Result.success("处理举报成功");
            } else {
                log.warn("处理举报失败, ID: {}", reportId);
                return Result.failed("处理举报失败，可能是举报不存在");
            }
        } catch (Exception e) {
            log.error("处理举报出错: {}", e.getMessage(), e);
            return Result.failed("处理举报失败: " + e.getMessage());
        }
    }
} 